#!/bin/sh
# 备份MySQL数据库
# 基于Xtrabackup完全备份
MySQLDir=/usr/local/mysql
MyCnf=/etc/my.cnf
XtrabackupDir=/usr/local/xtrabackup
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:$MySQLDir/bin:$XtrabackupDir/bin"

User=root
Password=root
MailTo=dongsong@aifan.com
Day=$(date +%Y-%m-%d)
SaveDays=7
BackupRoot=/data/backup
FullDir=$BackupRoot/$Day/full
LogFile=$BackupRoot/$Day/full.log
MailFile=$BackupRoot/$Day/mail
HostName=$(hostname)
IPList=$(ifconfig | grep -v '127.0.0.1' | sed 's/addr://g' | grep 'inet ' | awk '{print $2}')

find $BackupRoot -maxdepth 1 -type d -mtime +$SaveDays -exec rm -rf {} \;
test -d $BackupRoot/$Day || mkdir -p $BackupRoot/$Day

cat > $MailFile << EOF
$HostName
$IPList
EOF
cat $MailFile

innobackupex --defaults-file=$MyCnf --no-timestamp --user $User --password $Password $FullDir 2>> $LogFile
test "$?" -ne 0 && cat $MailFile $LogFile | mail -s "Full backup failed!" $MailTo
